﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class ImportForeignLogEdit : Form
    {
        public ImportForeignLogEdit()
        {
            InitializeComponent();
        }

        private ImportForeignLog selectedImportForeignLog = new ImportForeignLog();
        public ImportForeignLog SelectedImportForeignLog
        {
            get { return selectedImportForeignLog; }
            set { selectedImportForeignLog = value; }
        }

        private EventScavengerDAL eventScavengerDAL;

        public DialogResult Show(EventScavengerDAL eventScavengerDAL)
        {
            this.eventScavengerDAL = eventScavengerDAL;
            return ShowDialog();
        }

        private void ImportForeignLogEdit_Load(object sender, EventArgs e)
        {
            LoadCombos();
            if (selectedImportForeignLog != null)
            {
                cboCollector.Text = selectedImportForeignLog.CollectorName;
                txtSQLServer.Text = selectedImportForeignLog.SQLServer;
                txtDatabase.Text = selectedImportForeignLog.Database;
                cboMachineLog.Text = selectedImportForeignLog.Machine + "\\" + selectedImportForeignLog.LogName;
                chkEnabled.Checked = selectedImportForeignLog.Enabled;
            }
        }

        private void LoadCombos()
        {
            List<Collector> collectors = eventScavengerDAL.GetCollectorList();
            foreach (Collector collector in collectors)
            {
                cboCollector.Items.Add(collector);
            }
            List<MachineLogEntry> machineLogs = eventScavengerDAL.GetMachineLogList(false);
            foreach (MachineLogEntry mle in machineLogs)
            {
                cboMachineLog.Items.Add(mle);
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (cboCollector.SelectedIndex == -1)
            {
                MessageBox.Show("You must specify a collector!", "Collector", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboCollector.Focus();
            }
            else if (txtSQLServer.Text.Length == 0)
            {
                MessageBox.Show("You must specify a SQL server!", "SQL server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtSQLServer.Focus();
            }
            else if (txtDatabase.Text.Length == 0)
            {
                MessageBox.Show("You must specify a database!", "Database", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtDatabase.Focus();
            }
            else if (cboMachineLog.SelectedIndex == -1)
            {
                MessageBox.Show("You must specify the machine-log!", "Machine-log", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cboMachineLog.Focus();
            }
            else
            {
                selectedImportForeignLog.CollectorId = ((Collector)cboCollector.SelectedItem).Id;
                selectedImportForeignLog.CollectorName = cboCollector.Text;
                selectedImportForeignLog.SQLServer = txtSQLServer.Text;
                selectedImportForeignLog.Database = txtDatabase.Text;
                MachineLogEntry mle = (MachineLogEntry)cboMachineLog.SelectedItem;
                selectedImportForeignLog.MachineLogId = mle.MachineLogID;
                selectedImportForeignLog.Machine = mle.Machine;
                selectedImportForeignLog.LogName = mle.LogName;
                selectedImportForeignLog.Enabled = chkEnabled.Checked;
                DialogResult = DialogResult.OK;
                Close();
            }

        }

        private void cmdTest_Click(object sender, EventArgs e)
        {
            int recordCount = 0;
            try
            {
                EventScavengerDAL eventScavengerDAL = new EventScavengerDAL();
                eventScavengerDAL.Server = txtSQLServer.Text;
                eventScavengerDAL.Database = txtDatabase.Text;
                eventScavengerDAL.UserName = "";
                eventScavengerDAL.Password = "";
                eventScavengerDAL.CommandTimeout = Properties.Settings.Default.SQLCMDTimeOut;
                eventScavengerDAL.SetConnection();
                recordCount = eventScavengerDAL.TestConnection();
                MessageBox.Show("Success! What else did you expect? ;)\r\nThere are " + recordCount.ToString() + " record(s) in the table", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
